package Dao;

import Utils.JdbcTemplateUtils;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import pojo.projects;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

public class projectDao {
    JdbcTemplate jdbcTemplate= JdbcTemplateUtils.getJdbcTemplate();
    public List<Map<String,Object>> showProject(){
        String sql="select * from project";
        List<Map<String, Object>> projects = jdbcTemplate.queryForList(sql);
        return projects;
    }
    public List<projects> getProjectById(int id){
        String sql="select * from project where id='"+id+"'";
        List<projects> query = jdbcTemplate.query(sql, new RowMapper<projects>() {
            @Override
            public projects mapRow(ResultSet resultSet, int i) throws SQLException {
                projects projects = new projects();
                projects.setId(resultSet.getInt("id"));
                projects.setProjects(resultSet.getString("projectname"));
                return projects;
            }
        });
        return query;
    }
    public void addProject(projects projects){
        String sql="insert into project (id,projectname) values (?,?)";
        Object[] obj=new Object[2];
        obj[0]=projects.getId();
        obj[1]=projects.getProjects();
        jdbcTemplate.update(sql,obj);
        System.out.println("添加课程成功");
    }
    public void deleteProject(Integer id)
    {
        String sql="DELETE FROM project Where id="+id;
        jdbcTemplate.update(sql);
        System.out.println("删除成功");
    }

}
